local extension = Package("realgod")
extension.extensionName = "tianshu"

Fk:loadTranslationTable{
  ["realgod"] = "平局.守护神",
  ["tshu_rgod"] = "创世·神",
  ["tshu_long"] = "灭世·龙",
}

local U = require "packages/utility/utility"

local chuangshi = fk.CreateActiveSkill{
  name = "tshu_chuangshi",
  anim_type = "masochism",
  can_use = function(self, player)
    return  Fk:currentRoom().room_settings.gameMode ~= "tshu_mode"
  end,
  interaction = function()
    return UI.ComboBox {choices = {"tshu_zaowu","tshu_matai","tshu_jiushu","tshu_qiancheng"}}
  end,
  prompt = function (self, player)
    local ch = self.interaction.data
    return "#"..ch
  end,
  target_num = 0,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local ch = self.interaction.data

    --造物——技能
    if ch == "tshu_zaowu" then
    local cgens = room:getNGenerals(18)
    local gens = room:askForGeneral(player,cgens,5,false)
    local skills = {}
    local choices = {}
    for _, general_name in ipairs(gens) do
      local general = Fk.generals[general_name]
      local g_skills = {}
      for _, skill in ipairs(general.skills) do
          table.insertIfNeed(g_skills, skill.name)
      end
      for _, s_name in ipairs(general.other_skills) do
        local skill = Fk.skills[s_name]
          table.insertIfNeed(g_skills, skill.name)
      end
      table.insertIfNeed(skills, g_skills)
      if #choices == 0 and #g_skills > 0 then
        choices = {g_skills[1]}
      end
    end
    if #choices > 0 then
      local result = player.room:askForCustomDialog(player, self.name,
      "packages/tianshu/qml/ChooseGeneralSkillsBox2.qml", {
        gens, skills,1,#choices, "#tshu_zaowu-choice",true
      })
      if result ~= "" then
        choices = json.decode(result)
      local mark = player:getTableMark("@[tshu__tianshu]")
      for _, sk in ipairs({choices}) do
        table.forEach(sk, function(s) if not player:hasSkill(s) then table.insertIfNeed(mark,s) end end)
      end
      room:setPlayerMark(player,"@[tshu__tianshu]",mark)
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil,false)
      for _, s in ipairs(choices) do
        s = Fk.skills[s]
         if s:isInstanceOf(TriggerSkill) and type(s.events) == "table" and (table.contains(s.events,fk.GameStart) or table.contains(s.events, fk.GamePrepared)) then
           local events = {fk.GamePrepared, fk.GameStart}
           for i = #events, 1, -1 do
             if not table.contains(s.events, events[i]) then
               table.remove(events, i)
             end
           end
           for _, e in ipairs(events) do
             if s:triggerable(e, player, player, nil) then
               s:trigger(e, player, player, nil)
             end
           end
         end
       end
      end
      room:returnToGeneralPile(cgens)
      end
    elseif ch == "tshu_jiushu" then
      for _, p in ipairs(room.players) do
        room:resumePlayerArea(p, {Player.WeaponSlot, Player.ArmorSlot, Player.OffensiveRideSlot, Player.DefensiveRideSlot, Player.TreasureSlot, Player.JudgeSlot}) -- 全部恢复
        room:revivePlayer(p, false)
        room:setPlayerProperty(p, "kingdom", Fk.generals[p.general].kingdom)
        room:setPlayerProperty(p, "maxHp",Fk.generals[p.general].maxHp)
        room:setPlayerProperty(p, "hp",Fk.generals[p.general].hp)
        p:drawCards(5,self.name)
      end
    end
  end,
}
Fk:loadTranslationTable{
  ["tshu_chuangshi"] = "创世",
  [":tshu_chuangshi"] = "出牌阶段，你可以进行："..
  "<br>〖造物化身〗从任意张武将牌上获得或给予任意个技能；"..
  "<br>〖永恒救赎〗令全场角色复活并恢复状态到最佳值；"..
  "<br>〖创造之羽〗移动、控制、获得任意角色区域或牌堆的任意牌；"..
  "<br>〖虔诚十字〗变更其他角色武将、座次或身份。",
  ["#tshu_zaowu"] = "造物：从任意个武将牌上获得任意个技能。",


  ["$tshu_chuangshi1"] = "凡是求的就必得到，愿的就必实现，死去的就必复活。",
  ["$tshu_chuangshi2"] = "凡人之躯，也敢比肩神明。",
  ["$tshu_chuangshi3"] = "神对你的爱，比你对他的爱多很多。",
  ["$tshu_chuangshi4"] = "假使有上帝，我怎能忍受我不是那上帝。",
}
local matai = fk.CreateTriggerSkill{
  name = "tshu_matai",
  events = {},
  frequency = Skill.Compulsory,
  anim_type = "special",
}
Fk:loadTranslationTable{
  ["tshu_matai"] = "马太",
  [":tshu_matai"] = "锁定技，当你死亡时，你立即复活。",

  ["$tshu_matai"] = "能打败我的只有我,或许..还有他。",
}

local longyin = fk.CreateActiveSkill{
  name = "tshu_longyin",
  anim_type = "masochism",
  target_num = 0,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
  end,
}
Fk:loadTranslationTable{
  ["tshu_longyin"] = "龙引",
  [":tshu_longyin"] = "出牌阶段，你可以转化为“灭世·龙”形态并对全场触发一次“游戏开始”时机。",


  ["$tshu_longyin"] = "你们的主,降临了。",
}
local god_win = fk.CreateActiveSkill{ name = "tshu_rgod__sajialiya_win_audio" }
god_win.package = extension
Fk:addSkill(god_win)
local god = General(extension, "tshu_rgod__sajialiya", "god",9)
god:addSkill(chuangshi)
god:addSkill(matai)
god:addSkill(longyin)
Fk:loadTranslationTable{
  ["tshu_rgod__sajialiya"] = "撒迦利亚",
  ["#tshu_rgod__sajialiya"] = "创世·神",

  ["$tshu_rgod__sajialiya_win_audio"] = "要有光！",
}
local long = General(extension, "tshu_long__sajialiya", "god",9)
Fk:loadTranslationTable{
  ["tshu_long__sajialiya"] = "亚利迦撒",
  ["#tshu_long__sajialiya"] = "灭世·龙",

  ["$tshu_long__sajialiya_win_audio"] = "汉室复兴，指日可待！",
}
return extension